package com.cskaoyan.service.wx;

import com.cskaoyan.bean.po.MarketAddress;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.bean.wx.address.WxAddressDateVo;
import com.cskaoyan.config.exception.LoginAuthenException;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.UserIdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public WxAddressDateVo queryAll() {

        PageHelper.startPage(1,Integer.MAX_VALUE);

        WxAddressDateVo wxAddressDateVo = new WxAddressDateVo();

        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketAddresses);

        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();

        wxAddressDateVo.setList(marketAddresses);
        wxAddressDateVo.setLimit(Integer.MAX_VALUE);
        wxAddressDateVo.setPage(1);
        wxAddressDateVo.setPages(pages);
        wxAddressDateVo.setTotal((int) total);


        return wxAddressDateVo;
    }

    @Override
    public int inset(MarketAddress marketAddress) throws LoginAuthenException {
        Date date = new Date();

        if(marketAddress.getId() == 0) {

            if(marketAddress.getIsDefault() == false) {
                Integer userId = UserIdUtil.getUserIdFromSubjectThrowException();

                marketAddress.setUserId(userId);

                marketAddress.setAddTime(date);

                marketAddress.setDeleted(false);

                marketAddressMapper.insert(marketAddress);
            }else {
                MarketAddress marketAddress1 = new MarketAddress();

                marketAddress1.setIsDefault(false);

                MarketAddressExample example = new MarketAddressExample();

                MarketAddressExample.Criteria criteria = example.createCriteria();

                criteria.andDeletedEqualTo(false);

                marketAddressMapper.updateByExampleSelective(marketAddress1,example);

                Integer userIdFromSubject = UserIdUtil.getUserIdFromSubjectThrowException();

                marketAddress.setUserId(userIdFromSubject);

                marketAddress.setAddTime(date);

                marketAddress.setDeleted(false);

                marketAddressMapper.insert(marketAddress);
            }
        }else {

            if(marketAddress.getIsDefault() == false) {

                marketAddress.setUpdateTime(date);

                marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
            }else{
                MarketAddress marketAddress1 = new MarketAddress();

                marketAddress1.setIsDefault(false);

                MarketAddressExample example = new MarketAddressExample();

                MarketAddressExample.Criteria criteria = example.createCriteria();

                criteria.andDeletedEqualTo(false);

                marketAddressMapper.updateByExampleSelective(marketAddress1,example);

                marketAddress.setUpdateTime(date);

                marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
            }

        }
        return marketAddress.getId();
    }

    @Override
    public void delete(MarketAddress marketAddress) {

        marketAddress.setDeleted(true);

        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);

    }

    @Override
    public MarketAddress select(Integer id) {

        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);

        return marketAddress;
    }
}
